/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c) 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * @original author: jin Chen 
 * @author: jin Chen 
 * @date: Jan 17, 2007$
 * @version: 1.0
 */
package edu.psu.geovista.data.utils;

//import edu.psu.geovista.common.io.readers.JShapeFileDataReader;

import edu.psu.geovista.data.shapefile.JShapeFileDataReader;
import edu.psu.geovista.data.shapefile.ShapeFileProjection;
import edu.psu.geovista.db.dbase.DBaseFile;

import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import epoviz.trex.common.io.csv.CSVAppDataReader;
import epoviz.trex.common.io.csv.CSVFactory;
//import test.edu.psu.geovista.app.pcp.common.TestGeoStuioComponents;

public class GeoVistaDataTool  {  //extends BasicDataTool

    /*******************************************************************************************************
     *                shp data
     *******************************************************************************************************/
    /**
     *  
     *  
     *   
     * @param fn
     * @param applyProjection
     * @return    
     */
    public static Object[] loadShapeData( String fn, boolean applyProjection ){
        if(!isExist(fn)){
           new IllegalArgumentException(GeoVistaDataTool.class.getName() + fn+ " does not exist!");
        }
       // String sfn = FileService.removeExtension(fn);//simple file name without extension

            try {
               // logger.info("Shape data "+fn+" is not associate to any attribute data ( .csv, .dbf), a dummy attribute data is generated.");
               return  loadSingleShapeFile(fn,applyProjection);
            } catch (FileNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        //}
        System.err.println(" Fail to load shape data "+fn);
        return new Object[0];
    }
    static boolean isExist(String fn){
        return new File(fn).exists() ;
    }
     /**
     *  
     *  
     * @param fn, name of the shape data file, it must be a .shp file
     * @param applyProjection
     * @return         
     * @throws FileNotFoundException
     */
     static  Object[] loadSingleShapeFile(String fn, boolean applyProjection) throws FileNotFoundException {
         Shape[] shapes = loadSingleShapeFileAsShapeArray(fn, applyProjection);
         // 
         String[] attrnames=new String[]{"dummyData","shp"};
            double[] empty=new double[shapes.length];
            for (int i=0;i<empty.length ;i++){
                empty[i]=0;
            }
            Object[] data=new Object[]{attrnames,empty,shapes};
           return data;
     }



    /**
     *
     *  
     *  
     * @param fn, name of the shape data file, it must be a .shp file
     * @param applyProjection
     * @return        
     * @throws FileNotFoundException
     */
    public static  Shape[] loadSingleShapeFileAsShapeArray(String fn, boolean applyProjection) throws FileNotFoundException {
        JShapeFileDataReader shpR=new JShapeFileDataReader();
        Shape[] shp=(Shape[]) shpR.readShapeFile(fn);
        if(applyProjection )  {
            return projectData(shp);
        }
        return shp;

     }


    public static Shape[] projectData(Shape[] shp) {
        ShapeFileProjection project=new ShapeFileProjection();
        return project.doProjection(shp);
    }







    public static Object[] loadDBaseFile(String fn){
         DBaseFile dbaseFile2 = null; //, dataArray);
        try {
            dbaseFile2 = new DBaseFile(fn);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        Object[] dataArray  = dbaseFile2.getDataSet();
        return dataArray;
    }


   public static Object[]  loadCSVData(String  file) {

       // ShapeFileDataReader sfdr=new  ShapeFileDataReader();
        CSVAppDataReader csv= CSVFactory.getCSVAppDataReader() ;

        Object[] dataSet = csv.readFile(file) ;


        return dataSet;
    }
    

    public static class DataFileNames{
        public String shpname;
        public String dbfname;
        public String csvname;
        public String metaname;
    }



}